package cn.com.nes.site.service.workOrder.impl;

import cn.com.nes.common.exception.MissBusinessParameterException;
import cn.com.nes.common.util.CopyUtils;
import cn.com.nes.common.web.CommonResult;
import cn.com.nes.mybatis.agent.ope.entity.WorkOrderCancelDetail;
import cn.com.nes.mybatis.agent.ope.mapper.WorkOrderCancelDetailMapper;
import cn.com.nes.mybatis.callcenter.entity.WorkOrderInfo;
import cn.com.nes.site.entity.em.WorkOrderStautsEnum;
import cn.com.nes.site.service.workOrder.WorkOrderCancelDetailService;
import cn.com.nes.site.service.workOrder.WorkOrderService;
import cn.com.nes.site.service.workOrder.bo.InsertCancelDetailBO;
import cn.com.nes.site.service.workOrder.bo.QueryCancelDetailPageBO;
import cn.com.nes.site.service.workOrder.dto.WorkOrderCancelDetailDTO;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工单批量撤销记录详情(WorkOrderCancelDetail)表服务实现类
 */
@Slf4j
@Service
public class WorkOrderCancelDetailServiceImpl extends ServiceImpl<WorkOrderCancelDetailMapper, WorkOrderCancelDetail> implements WorkOrderCancelDetailService {

    @Resource
    private WorkOrderService workOrderService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertCancelDetail(Long cancelRecordId, List<InsertCancelDetailBO> boList) {
        if (ObjectUtil.isNull(cancelRecordId)) {
            throw new MissBusinessParameterException("cancelRecordId must not null");
        }
        if (CollectionUtil.isEmpty(boList)) {
            throw new MissBusinessParameterException("InsertCancelDetailBOList must not empty");
        }

        WorkOrderCancelDetail workOrderCancelDetail;
        for (InsertCancelDetailBO cancelDetailBO : boList) {
            workOrderCancelDetail = new WorkOrderCancelDetail();
            BeanUtils.copyProperties(cancelDetailBO, workOrderCancelDetail);
            workOrderCancelDetail.setRecordId(cancelRecordId);
            workOrderCancelDetail.setDeleted(false);
            workOrderCancelDetail.setGmtCreateId(cancelDetailBO.getOperateUserId());
            workOrderCancelDetail.setGmtCreate(cancelDetailBO.getOperateDate());
            workOrderCancelDetail.setGmtModifiedId(cancelDetailBO.getOperateUserId());
            workOrderCancelDetail.setGmtModified(cancelDetailBO.getOperateDate());

            int rows = this.baseMapper.insert(workOrderCancelDetail);
            if (rows < 1) {
                log.error("新增撤销记录详情失败");
            }
        }
    }

    @Override
    public CommonResult<?> queryCancelDetailPage(QueryCancelDetailPageBO queryBO) {
        if (ObjectUtil.isNull(queryBO.getCancelRecordId())) {
            throw new MissBusinessParameterException("cancelRecordId must not null");
        }

        // 组装查询参数
        QueryWrapper<WorkOrderCancelDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id", queryBO.getCancelRecordId());
        queryWrapper.eq("deleted", false);
        queryWrapper.orderByDesc("gmt_create");
        // 准备分页参数
        Page<WorkOrderCancelDetail> queryPage = new Page<>(queryBO.getPageid(), queryBO.getPagesize());
        // 执行查询SQL
        IPage<WorkOrderCancelDetail> workOrderCancelDetailIPage = this.baseMapper.selectPage(queryPage, queryWrapper);
        // 转换查询结果
        List<WorkOrderCancelDetailDTO> records = CopyUtils.copyList(workOrderCancelDetailIPage.getRecords(), WorkOrderCancelDetailDTO.class);

        // 处理返回值
        for (WorkOrderCancelDetailDTO record : records) {
            WorkOrderInfo workOrderInfo = workOrderService.getWorkOrderInfo(record.getWorkOrderId());
            if (ObjectUtil.isNull(workOrderInfo)) {
                continue;
            }
            record.setStatus(String.valueOf(workOrderInfo.getStatus()));
            record.setStatusName(WorkOrderStautsEnum.getVal(workOrderInfo.getStatus()));
        }

        // 组装返回值
        Page<WorkOrderCancelDetailDTO> page = new Page<>(queryBO.getPageid(), queryBO.getPagesize());
        page.setRecords(records);
        return CommonResult.page(page);
    }
}
